package com.ht.managermentserver.utils;

import com.ht.managermentserver.sysenum.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 统一返回处理
 *
 * @author czy
 * @package com.ht.managermentserver.utils
 * @date 2020/6/3 13:32
 */
@Data
@AllArgsConstructor
public class JsonResult<T> {

  /** @JSONField(serializeUsing = FastJsonDefaultSerializer.class) */
  private ErrorCode errorCode;

  private String errorMessage;
  private T returnData;

  public JsonResult(final ErrorCode errorCode, final String errorMessage) {

    this.errorCode = errorCode;

    this.errorMessage = errorMessage;

    this.returnData = null;
  }

  public static <T> JsonResult<T> create(final String errorMessage, final ErrorCode errorCode) {

    return new JsonResult<T>(errorCode, errorMessage);
  }

  public static <T> JsonResult<T> createSuccess(final T returnData) {
    return new JsonResult<T>(ErrorCode.SUCCESS, "获取成功",returnData);
  }
  public static <T> JsonResult<T> createError(String errorMessage) {
    return new JsonResult<T>(ErrorCode.ERROR, errorMessage);
  }


  public static <T> JsonResult<T> create(
      final String errorMessage, final ErrorCode errorCode, final T returnData) {

    return new JsonResult<T>(errorCode, errorMessage, returnData);
  }

  @Override
  public String toString() {
    return "JsonResult{"
        + "errorCode="
        + errorCode
        + ", errorMessage='"
        + errorMessage
        + '\''
        + ", returnData="
        + returnData
        + '}';
  }

  public String toJsonString() {
    return JSONUtil.toJsonString(this);
  }
}
